package com.car.dao.impl;

import com.car.dao.EmployeeDao;
import com.car.domain.Employee;
import com.car.utils.JDBCUtil;
import com.car.utils.JDBCUtils;

import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {
    /**
     * 新增
     *
     * @param employee 泛型
     */
    @Override
    public int add(Employee employee) throws Exception {
        String sql = "insert into t_employee (name, age, gender, work_id, salary) \n" +
                "VALUE(?,?,?,?,?)";
        return JDBCUtil.update(sql, employee.getName(), employee.getAge()
                , employee.getGender(), employee.getWorkId(), employee.getSalary());
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from t_employee where id = ? ";
        return JDBCUtil.update(sql, id);
    }

    /**
     * 修改
     *
     * @param employee 泛型
     */
    @Override
    public int update(Employee employee) throws Exception {
        String sql = "update t_employee set name = ?,age=?,gender=?,work_id=?,salary=?" +
                " where id = ?";
        return JDBCUtil.update(sql, employee.getName(), employee.getAge(), employee.getGender()
                , employee.getWorkId(), employee.getSalary(), employee.getId());
    }

    /**
     * 通过主键id查询
     *
     * @param id 主键id
     * @return 实体类
     */
    @Override
    public Employee queryById(int id) throws Exception {
        String sql = "select * from t_employee where id = ?";
        return JDBCUtil.query(sql, Employee.class, id);
    }

    /**
     * 查询所有记录
     *
     * @return 实体类集合
     */
    @Override
    public List<Employee> queryAll() throws Exception {
        String sql = "select * from t_employee";
        return JDBCUtil.queryList(sql, Employee.class);
    }

    @Override
    public Employee findByWorkId(String worId) throws Exception {
        String sql = "select * from t_employee where work_id=?";
        return JDBCUtils.query(sql,Employee.class,worId);
    }
}
